package com.hongxun.design.adapters;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.activitys.DotDesignActivity;
import com.hongxun.design.bean.SetPointInfItemBean;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.PopwindowUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/4/29.
 */
public class DotDesignAdapter extends BaseAdapter {
    private List<SetPointInfItemBean> beanList;
    private LayoutInflater inflater;
    private Activity context;
    public int mposition = 0;
    //MyApp.currExploration.getApapterStates();

    public DotDesignAdapter(List<SetPointInfItemBean> beanList, Activity context) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.beanList = beanList;
    }

    public List<SetPointInfItemBean> getData() {
        return beanList;
    }

    public void setData(int position, String msg, String s, int i) {
        beanList.get(position).setValue(msg);
        beanList.get(position).setValueCode(s);
        beanList.get(position + 1).setValue(i + "");
        beanList.get(position + 1).setValueCode(i + "");
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int i) {
        return beanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getViewTypeCount() {
        return 9;
    }

    @Override
    public int getItemViewType(int position) {
        int type = beanList.get(position).getItemStyle();
        return type;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        int ViewType = getItemViewType(position);
        JumpHolder jumpHolder = null;
        switch (ViewType) {
            case Constant.newStyle1://典设
            case Constant.newStyle2://清空材料
                convertView = inflater.inflate(R.layout.listview_item_setpoint_jumpstyle, null);
                jumpHolder = new JumpHolder(convertView);
                convertView.setTag(jumpHolder);
                break;
        }
        final SetPointInfItemBean bean;
        try {
            switch (ViewType) {
                case Constant.newStyle1://加载模块、材料
                    SetTypeface.set(jumpHolder.modify, IconSourse.ADD);
                    bean = beanList.get(position);
                    jumpHolder.title.setText(bean.getName());
                    jumpHolder.value.setText(bean.getValue());
                    jumpHolder.modify.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (!MyApp.ReadOnly) {
                                String version_id = MyApp.currExploration.getCodeset_id();
                                Map<String, Object> map = new HashMap<>();
                                map.put("cid", version_id);
                                map.put("type", "点位");
                                PopwindowUtil.openPop((TextView) v, (Activity) v.getContext(), DataUtil.getMaterialItem(map));
                            }
                        }
                    });
                    // 选择典设
                    jumpHolder.item.setOnClickListener(bean.getChoiceModelListener());
                    break;
                case Constant.newStyle2://清除所有材料（暂时取消）
                    jumpHolder.right.setVisibility(View.GONE);
                    jumpHolder.modify.setVisibility(View.GONE);
//                    SetTypeface.set(jumpHolder.modify, IconSourse.DELECT);
                    bean = beanList.get(position);
                    jumpHolder.title.setText(bean.getName());
                    jumpHolder.value.setText(bean.getValue());
                    jumpHolder.value.setPadding(0, 0, 30, 0);
                    jumpHolder.modify.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            AlertDialog.Builder builder = new AlertDialog.Builder(context);
                            builder.setTitle("操作提示");
                            builder.setMessage("是否清空当前点位所有材料?");
                            builder.setNegativeButton("否", null);
                            builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                            builder.create();
                            builder.show();
                        }
                    });
                    break;
            }
        } catch (Exception e) {
            MyLog.L(MyApp.AppName + "适配错误：" + e.toString());
        }
        return convertView;
    }

    public class JumpHolder {
        public TextView modify, title, value, right;
        public RelativeLayout item;

        JumpHolder(View convertView) {
            item = (RelativeLayout) convertView.findViewById(R.id.jump_item);
            modify = (TextView) convertView.findViewById(R.id.modify);
            title = (TextView) convertView.findViewById(R.id.jump_title);
            value = (TextView) convertView.findViewById(R.id.jump_value);
            right = (TextView) convertView.findViewById(R.id.jump_right);
            SetTypeface.set(right, IconSourse.RIGHTCHOICE);
            SetTypeface.set(modify, IconSourse.EDIT);
        }
    }

}
